package HTMLParser.HTMLTag;

import Parser.Token;

/*******************************************************************************
 * This class deals with Image tags. Its main function is to extract and store
 * the different variables from an <img> tag. It stores the path to the image,
 * and the height/width of the image.
 * @author James Johnstone
 * @version FINAL
 *******************************************************************************/
public class Image extends Tag {

    private String path;
    private int height;
    private int width;

    /*******************************************************************************
     * Constructor for the Image class. Takes one variable which is a Token
     * representing the HTML Tag (example: <img src="image.jpg" />). It then
     * extracts the src, height and width tags and stores them appropriately.
     * @param t A Token representing a HTML IMG tag.
     *******************************************************************************/
    public Image(Token t) {
        super(t, "Image");
        String tokenString = t.getToken().toLowerCase();
        if (tokenString.contains("src=" + '"')) {
            int first = tokenString.indexOf("src=" + '"') + 5;
            int last = tokenString.indexOf("" + '"', first);
            path = tokenString.substring(first, last);
        } else {
            errorLog.logError("Image tag does not contain URL.");
        }

        if (t.getToken().toLowerCase().contains("height=" + '"')) {
            int first = tokenString.indexOf("height" + '"') + 7;
            int last = tokenString.indexOf("" + '"', first);
            height = Integer.parseInt(tokenString.substring(first, last));
        } else {
            height = -1;
        }

        if (t.getToken().toLowerCase().contains("width=" + '"')) {
            int first = tokenString.indexOf("width=" + '"') + 6;
            int last = tokenString.indexOf("" + '"', first);
            width = Integer.parseInt(tokenString.substring(first, last));
        } else {
            width = -1;
        }
    }

    /***************************************************************************
     * Gets the path to the image.
     * @return  A String containing filename of the image.
     ***************************************************************************/
    public String getPath() {
        return path;
    }

    /***************************************************************************
     * Gets the image height
     * @return  An int containing the image height.
     ***************************************************************************/
    public int getHeight() {
        return height;
    }

    /***************************************************************************
     * Gets the image width
     * @return  A String containing the name of the font specified within
     * the Token.
     ***************************************************************************/
    public int getWidth() {
        return width;
    }
}

